<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    初识Redis |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-初始Redis" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  初识Redis
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2021/02/05/%E5%88%9D%E5%A7%8BRedis/" class="article-date">
  <time datetime="2021-02-05T08:45:02.000Z" itemprop="datePublished">2021-02-05</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Redis/">Redis</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.8k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">9分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="什么是Redis？"><a href="#什么是Redis？" class="headerlink" title="什么是Redis？"></a>什么是Redis？</h2><p>Redis 是一个使用 C 语言写成的高性能非关系型数据库。它支持存储的多种数据 类型，包括<strong>string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和 hash（哈希类型）</strong>。Redis的数据都基于缓存的，所以很快，每秒可以处理超过 10万次读写操 作。为保证数据的安全不丢失，也可以实现数据写入磁盘中，而且Redis的操作是原子性（对数据的更改要么全部执行，要么全部不执行 ）的。</p>
<h2 id="优缺点"><a href="#优缺点" class="headerlink" title="优缺点"></a>优缺点</h2><ul>
<li>优点：<ul>
<li>读写性能优异，Redis能读的速度是11万次/s,写的速度是81000次/s</li>
<li>支持数据的持久化（AOF和RDB）</li>
<li>支持事务，redis所有操作都是原子性的</li>
<li>数据结构丰富</li>
</ul>
</li>
<li>缺点：<ul>
<li>数据库容量受到物理内存的限制</li>
<li>不具备恢复功能，最怕主机宕机</li>
</ul>
</li>
</ul>
<h2 id="Redis为什么这么快"><a href="#Redis为什么这么快" class="headerlink" title="Redis为什么这么快"></a>Redis为什么这么快</h2><ol>
<li>完全基于内存，速度非常快</li>
<li>采用单线程，避免了上下文切换所耗费的时间，也不会有各种锁问题的性能消耗</li>
<li>使用多路I/O复用模型，非阻塞IO</li>
<li>底层模型是Redis自己构建了VM机制</li>
</ol>
<a id="more"></a>
<h2 id="Redis基本命令"><a href="#Redis基本命令" class="headerlink" title="Redis基本命令"></a>Redis基本命令</h2><p>清除当前数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flushdb</span><br></pre></td></tr></table></figure>

<p>清除所有数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flushall</span><br></pre></td></tr></table></figure>

<p>设置过期时间</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- key在10秒后过期，一般用于设置用户的cookie</span></span><br><span class="line">expire name 10</span><br><span class="line"><span class="comment">-- 查看当前key 的剩余时间</span></span><br><span class="line">ttl name</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 判断key是否存在</span></span><br><span class="line">exists name</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 移动当前key到某个数据库</span></span><br><span class="line">move key db</span><br><span class="line"><span class="comment">-- 删除当前key</span></span><br><span class="line">del key</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看key 的类型</span></span><br><span class="line">type key</span><br></pre></td></tr></table></figure>

<h2 id="String类型的使用"><a href="#String类型的使用" class="headerlink" title="String类型的使用"></a>String类型的使用</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; set stu zhangsan</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get stu</span><br><span class="line">"zhangsan"</span><br><span class="line">127.0.0.1:6379&gt; type stu</span><br><span class="line">string</span><br><span class="line">127.0.0.1:6379&gt; exists stu</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 追加字符串，若果没有当前key，则set该key</span></span><br><span class="line">127.0.0.1:6379&gt; append stu name</span><br><span class="line">(integer) 12</span><br><span class="line">127.0.0.1:6379&gt; get stu</span><br><span class="line">"zhangsanname"</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 获取字符串长度</span></span><br><span class="line">127.0.0.1:6379&gt; strlen stu</span><br><span class="line">(integer) 12</span><br></pre></td></tr></table></figure>

<h3 id="incr-和decr"><a href="#incr-和decr" class="headerlink" title="incr 和decr"></a>incr 和decr</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; clear</span><br><span class="line">127.0.0.1:6379&gt; set views 0</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get views</span><br><span class="line">"0"</span><br><span class="line">127.0.0.1:6379&gt; incr views</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; get views</span><br><span class="line">"1"</span><br><span class="line">127.0.0.1:6379&gt; incr views</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; get views</span><br><span class="line">"2"</span><br><span class="line">127.0.0.1:6379&gt; decr views</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; decr views</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; decr views</span><br><span class="line">(integer) -1</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 加步长</span></span><br><span class="line">127.0.0.1:6379&gt; incrby views 10</span><br><span class="line">(integer) 9</span><br><span class="line">127.0.0.1:6379&gt; incrby views 10</span><br><span class="line">(integer) 19</span><br><span class="line">127.0.0.1:6379&gt; decrby views 5</span><br><span class="line">(integer) 14</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) "stu"</span><br><span class="line">2) "views"</span><br><span class="line">127.0.0.1:6379&gt; get stu</span><br><span class="line">"zhangsanname"</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 字符串范围</span></span><br><span class="line">127.0.0.1:6379&gt; getrange stu 0 4</span><br><span class="line">"zhang"</span><br><span class="line">127.0.0.1:6379&gt; getrange stu 0 -1</span><br><span class="line">"zhangsanname"</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 替换</span></span><br><span class="line">127.0.0.1:6379&gt; setrange stu 4 xxx</span><br><span class="line">(integer) 12</span><br><span class="line">127.0.0.1:6379&gt; get stu</span><br><span class="line">"zhanxxxnname"</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>setex</strong> (set with expire) 设置过期时间</li>
<li><strong>setnx</strong> (set if not eixst) 不存在再设置，在分布式锁中会常使用</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; setex key2 30 "hello"</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; ttl key2</span><br><span class="line">(integer) 23</span><br><span class="line">127.0.0.1:6379&gt; setnx mykey "jin"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; setnx mykey "yolin"</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; get mykey</span><br><span class="line">"jin"</span><br><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) "stu"</span><br><span class="line">2) "mykey"</span><br><span class="line">3) "views"</span><br></pre></td></tr></table></figure>

<h3 id="mset和mget"><a href="#mset和mget" class="headerlink" title="mset和mget"></a>mset和mget</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 同时设置多个值</span></span><br><span class="line">127.0.0.1:6379&gt; mset k1 v1 k2 v2 k3 v3</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) "k1"</span><br><span class="line">2) "k2"</span><br><span class="line">3) "k3"</span><br><span class="line"><span class="comment">-- 同时获取多个值</span></span><br><span class="line">127.0.0.1:6379&gt; mget k1 k2 k3</span><br><span class="line">1) "v1"</span><br><span class="line">2) "v2"</span><br><span class="line">3) "v3"</span><br><span class="line">127.0.0.1:6379&gt; mset k1 v1 </span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; msetnx k1 v1</span><br><span class="line">(integer) 0</span><br><span class="line"><span class="comment">-- msetnx是一个原子性操作，要么都成功，要么都失败</span></span><br><span class="line">127.0.0.1:6379&gt; msetnx k1 v1 k4 v4</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; get k4</span><br><span class="line">(nil)</span><br><span class="line">127.0.0.1:6379&gt;</span><br></pre></td></tr></table></figure>

<h3 id="getset"><a href="#getset" class="headerlink" title="getset"></a>getset</h3><p>先get后set</p>
<ul>
<li>如果不存在值，返回nil，再set</li>
<li>存在值，get原来值，set新的值<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; getset db redis</span><br><span class="line">(nil)</span><br><span class="line">127.0.0.1:6379&gt; get db</span><br><span class="line">"redis"</span><br><span class="line">127.0.0.1:6379&gt; getset db mongodb</span><br><span class="line">"redis"</span><br><span class="line">127.0.0.1:6379&gt; get db</span><br><span class="line">"mongodb"</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="Set类型的使用"><a href="#Set类型的使用" class="headerlink" title="Set类型的使用"></a>Set类型的使用</h2><p>Set中的值是不重复的</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- set集合中添加元素</span></span><br><span class="line">127.0.0.1:6379&gt; sadd myset "hello"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset "yolin"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset "jin"</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看指定set的所有值</span></span><br><span class="line">127.0.0.1:6379&gt; smembers myset</span><br><span class="line">1) "yolin"</span><br><span class="line">2) "hello"</span><br><span class="line">3) "jin"</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 判断某个值是否在集合中</span></span><br><span class="line">127.0.0.1:6379&gt; sismember myset "jin"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sismember myset "hhhh"</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 获取set集合元素个数</span></span><br><span class="line">127.0.0.1:6379&gt; scard myset</span><br><span class="line">(integer) 3</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 元素不可重复</span></span><br><span class="line">127.0.0.1:6379&gt; sadd myset "hello"</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除元素</span></span><br><span class="line">127.0.0.1:6379&gt; srem myset hello</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; scard myset</span><br><span class="line">(integer) 2</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 随机抽选出一个元素</span></span><br><span class="line">127.0.0.1:6379&gt; srandmember myset</span><br><span class="line">"jin"</span><br><span class="line">127.0.0.1:6379&gt; srandmember myset</span><br><span class="line">"yolin"</span><br><span class="line">127.0.0.1:6379&gt; srandmember myset</span><br><span class="line">"yolin"</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 随机抽选指定个数的元素</span></span><br><span class="line">127.0.0.1:6379&gt; srandmember myset 2</span><br><span class="line">1) "yolin"</span><br><span class="line">2) "jin"</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 随机删除</span></span><br><span class="line">127.0.0.1:6379&gt; spop myset</span><br><span class="line">"jin"</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd myset "florence"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset "florence2"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset "florence3"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset2 "XXXX"</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="comment">-- 将一个指定的值移动到另一个set集合</span></span><br><span class="line">127.0.0.1:6379&gt; smove myset myset2 florence</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smembers myset2</span><br><span class="line">1) "XXXX"</span><br><span class="line">2) "florence"</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd key1 a</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key1 b</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key1 c</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key2 c</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key2 d</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key2 e</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="comment">-- 差集</span></span><br><span class="line">127.0.0.1:6379&gt; sdiff key1 key2</span><br><span class="line">1) "a"</span><br><span class="line">2) "b"</span><br><span class="line">127.0.0.1:6379&gt; sdiff key2 key1</span><br><span class="line">1) "d"</span><br><span class="line">2) "e"</span><br><span class="line"><span class="comment">-- 交集</span></span><br><span class="line">127.0.0.1:6379&gt; sinter key1 key2</span><br><span class="line">1) "c"</span><br><span class="line"><span class="comment">-- 并集</span></span><br><span class="line">127.0.0.1:6379&gt; sunion key1 key2</span><br><span class="line">1) "a"</span><br><span class="line">2) "b"</span><br><span class="line">3) "c"</span><br><span class="line">4) "e"</span><br><span class="line">5) "d"</span><br></pre></td></tr></table></figure>

<h2 id="Hash（哈希）类型的使用"><a href="#Hash（哈希）类型的使用" class="headerlink" title="Hash（哈希）类型的使用"></a>Hash（哈希）类型的使用</h2><p>Map集合，key-map（值是map集合），本质和String类型没有太大区别</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- set和get一个字段值</span></span><br><span class="line">127.0.0.1:6379&gt; hset myhash field1 jin</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hget myhash field1</span><br><span class="line">"jin"</span><br><span class="line"><span class="comment">-- set和get多个字段值</span></span><br><span class="line">127.0.0.1:6379&gt; hmset myhash field1 hello field2 world</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; hmget myhash field1 field2</span><br><span class="line">1) "hello"</span><br><span class="line">2) "world"</span><br><span class="line"><span class="comment">-- 获取全部值（键值对）</span></span><br><span class="line">127.0.0.1:6379&gt; hgetall myhash</span><br><span class="line">1) "field1"</span><br><span class="line">2) "hello"</span><br><span class="line">3) "field2"</span><br><span class="line">4) "world"</span><br><span class="line"><span class="comment">-- 只获得键</span></span><br><span class="line">127.0.0.1:6379&gt; hkeys myhash</span><br><span class="line">1) "field2"</span><br><span class="line">2) "field1"</span><br><span class="line"><span class="comment">-- 只获得值</span></span><br><span class="line">127.0.0.1:6379&gt; hvals myhash</span><br><span class="line">1) "world"</span><br><span class="line">2) "hello"</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除指定key</span></span><br><span class="line">127.0.0.1:6379&gt; hdel myhash field1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hgetall myhash</span><br><span class="line">1) "field2"</span><br><span class="line">2) "world"</span><br><span class="line"><span class="comment">-- 获取字段数量</span></span><br><span class="line">127.0.0.1:6379&gt; hlen myhash</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="comment">-- 判断hash中指定字段是否存在</span></span><br><span class="line">127.0.0.1:6379&gt; hexists myhash field1</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; hexists myhash field2</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hset myhash field3 5</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="comment">-- 指定增量</span></span><br><span class="line">127.0.0.1:6379&gt; hincrby myhash field3 1</span><br><span class="line">(integer) 6</span><br><span class="line"><span class="comment">-- 如果不存在则可以设置</span></span><br><span class="line">127.0.0.1:6379&gt; hsetnx myhash field4 hello </span><br><span class="line">(integer) 1</span><br><span class="line"><span class="comment">-- 存在则不能设置</span></span><br><span class="line">127.0.0.1:6379&gt; hsetnx myhash field4 world</span><br><span class="line">(integer) 0</span><br></pre></td></tr></table></figure>

<h2 id="Zset类型的使用"><a href="#Zset类型的使用" class="headerlink" title="Zset类型的使用"></a>Zset类型的使用</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建第一组元素mykey</span></span><br><span class="line">127.0.0.1:6379&gt; pfadd mykey a b c d e f g h i j</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; pfcount mykey</span><br><span class="line">(integer) 10</span><br><span class="line"><span class="comment">-- 创建第二组元素mykey2</span></span><br><span class="line">127.0.0.1:6379&gt; pfadd mykey2 i j z x c v b n m</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; pfcount mykey2</span><br><span class="line">(integer) 9</span><br><span class="line"><span class="comment">-- 合并两组mykey3（并集）</span></span><br><span class="line">127.0.0.1:6379&gt; pfmerge mykey3 mykey mykey2</span><br><span class="line">OK</span><br><span class="line"><span class="comment">-- 看元素的数量</span></span><br><span class="line">127.0.0.1:6379&gt; pfcount mykey3</span><br><span class="line">(integer) 15</span><br></pre></td></tr></table></figure>
      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Redis/" rel="tag">Redis</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2021/02/06/Redis%E4%BA%8B%E5%8A%A1%E5%8F%8A%E5%AE%9E%E7%8E%B0%E4%B9%90%E8%A7%82%E9%94%81/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Redis事务及实现乐观锁
          
        </div>
      </a>
    
    
      <a href="/2021/01/31/%E6%B3%A8%E8%A7%A3%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">注解实现原理</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>